﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    /// <summary>
    /// 供应商实现仓储
    /// </summary>
    public class VendorModelRepository : IVendorModelRepository
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext _context;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>

        public VendorModelRepository(AgricultureDbContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 供应商显示列表
        /// </summary>
        /// <param name="venDorCode">供应商编号</param>
        /// <param name="vendorName">供应商名称</param>
        /// <param name="responsibleName">负责人电话</param>
        /// <param name="markets">所属市场</param>
        /// <param name="salesman">服务人员</param>
        /// <param name="useState">使用状态</param>
        /// <returns></returns>
        public async Task<List<Vendormodel>> GetVendormodels()
        {
            var list = _context.Vendormodels.Where(c=>c.IsDel==false).ToList();
            return list;
        }
        /// <summary>
        /// 供应商删除
        /// </summary>
        /// <param name="loginUserId">删除人</param>
        /// <param name="idList">删除id</param>
        /// <returns></returns>
        public async Task<int> DeleteVendormodels(int loginUserId, params int[] idList)
        {
            if (idList.Length <= 0)
            {
                return 0;
            }
            var vendorms = _context.Vendormodels.Where(c => idList.Contains(c.VendorSnowId));
            foreach (var item in vendorms)
            {
                item.IsDel = true;
                item.UpdateTime = DateTime.Now;
                item.UpdateBy = loginUserId;
            }
            return _context.SaveChanges();
        }
		/// <summary>
		/// 供应商添加
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns></returns>
		public async Task<int> AddVendormodels(Vendormodel entity)
        {
            _context.Vendormodels.Add(entity);
            return _context.SaveChanges();
        }
		/// <summary>
		/// 供应商详情
		/// </summary>
		/// <param name="vendorSnowId"></param>
		/// <returns></returns>
		public async Task<Vendormodel> DetailVendormodel(int vendorSnowId)
        {
            var Detail = _context.Vendormodels.FirstOrDefault(c => c.VendorSnowId == vendorSnowId);
            return Detail;
        }
		/// <summary>
		/// 供应商修改
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<int> UpdateVendormodels(Vendormodel entity)
        {
            _context.Entry(entity).State = EntityState.Modified;
			
            return await _context.SaveChangesAsync();
        }
	}
}
